﻿var appConfig = {};
appConfig.toastrTitle = "Smarta.Admin - 提示";  //提示出现的标题
appConfig.lockTime = 0; //锁屏超时时间
appConfig.adminPath = "/Admin/"; //管理目录
appConfig.uploadPath = "/uploads/"; //上传文件的目录
appConfig.AutoSave = true;
appConfig.AutoSaveTime = 60000; //自动保存的时间 单位 : 毫秒
appConfig.AutoSaveName = "AutoSaveContent"; //自动保存的cookie名称

//系统编码字符串 配合后台使用 //如果涉及常用单词都在后面加s以防内容被替换
function StrEnCode(str) {
    str = str + "";
    //str = str.replace(/_FLAG_CONTENT_ENCODE_/g, ""); //替换编码头标示
    str = str.replace(/\[/g, "_MPBRACKETS_").replace(/]/g, "_MNBRACKETS_"); //[]
    str = str.replace(/{/g, "_BPBRACKETS_").replace(/}/g, "_BNBRACKETS_"); //{}
    str = str.replace(/\(/g, "_SPBRACKETS_").replace(/\)/g, "_SNBRACKETS_"); //()
    str = str.replace(/</g, "_TPBRACKETS_").replace(/>/g, "_TNBRACKETS_"); //<>
    str = str.replace(/%/g, "_PERCENTSS_").replace(/&/g, "_ANDS_"); //%&
    str = str.replace(/"/g, "_DOUBLEQUOTES_").replace(/'/g, "_APOSTROPHE_"); //"'
    str = str.replace(/\//g, "_SLASHS_").replace(/\\/g, "_BACKSLASHS_"); // /(斜杠) \反斜杠
    str = str.replace(/:/g, "_COLONS_").replace(/;/g, "_SEMICOLONS_"); // 冒号+分号
    str = str.replace(/\n/g, "_WRAPS_").replace(/\t/g, "_TABSS_").replace(/\r/g, "_ENTERS_").replace(/\r\n/g, "_ENTERSWRAPS_");
    str = str.replace(/©/g, "_COPYRIGHT_").replace(/=/g, "_EQUAL_"); // 版权符号 等于号
    str = str.replace(/\+/g, "_PLUS_").replace(/-/g, "_MINUS_"); //加号减号
    str = str.replace(/\*/g, "_MULTIPLICATION_"); //乘号
    str = str.replace(/\~/g, "_WAVE_").replace(/\!/g, "_SIGH_").replace(/\@/g, "_AITS_").replace(/\#/g, "_WELL_").replace(/\$/g, "_DOLLAR_").replace(/\^/g, "_WORDOFF_");
    return str;
}
//系统解码字符串 配合后台使用
function StrDeCode(str) {
    str = str + "";
    str = str.replace(/_MPBRACKETS_/g, "[").replace(/_MNBRACKETS_/g, "]"); //[]
    str = str.replace(/_BPBRACKETS_/g, "{").replace(/_BNBRACKETS_/g, "}"); //{}
    str = str.replace(/_SPBRACKETS_/g, "(").replace(/_SNBRACKETS_/g, ")"); //()
    str = str.replace(/_TPBRACKETS_/g, "<").replace(/_TNBRACKETS_/g, ">"); //<>
    str = str.replace(/_PERCENTSS_/g, "%").replace(/_ANDS_/g, "&"); //%&
    str = str.replace(/_DOUBLEQUOTES_/g, "\"").replace(/_APOSTROPHE_/g, "'"); //"'
    str = str.replace(/_SLASHS_/g, "/").replace(/_BACKSLASHS_/g, "\\"); // /(斜杠) :冒号  \反斜杠
    str = str.replace(/_COLONS_/g, ":").replace(/_SEMICOLONS_/g, ";"); // 冒号+分号
    str = str.replace(/_WRAPS_/g, "\n").replace(/_TABSS_/g, "\t").replace(/_ENTERS_/g, "\r").replace(/_ENTERSWRAPS_/g, "\r\n");
    str = str.replace(/_COPYRIGHT_/g, "©").replace(/_EQUAL_/g, "=");
    str = str.replace(/_PLUS_/g, "+").replace(/_MINUS_/g, "-");
    str = str.replace(/_MULTIPLICATION_/g, "*");
    str = str.replace(/_WAVE_/g, "~").replace(/_SIGH_/g, "!").replace(/_AITS_/g, "@").replace(/_WELL_/g, "#").replace(/_DOLLAR_/g, "$").replace(/_WORDOFF_/g, "^");
    return str;
}

function alert(msg, callback) {
    if (arguments.length == 1) {
        bootbox.alert(msg.toString());
    } else {
        bootbox.alert(msg.toString(), function () { setTimeout(function () { callback(); }, 300) });
    }
}

/*
通用提交 返回Json 默认使用Post提交
SynSubmit.apply(this, [1,2,3,4])  
第一个参数是url
第二个参数是action
第三个参数是data值
第四个参数是Url 的param 此参数会增加到Url上面
最后一个参数是回调
*/
function AjaxSubmit() {
    var args = Array.prototype.slice.call(arguments);
    var url_param = args[0] + "?action=" + args[1];
    if (args.length > 4) {
        for (var i = 3; i < args.length - 1; i++) {
            url_param += "&" + args[i];
        }
    }
    $.ajax({
        type: "POST",
        url: url_param,
        data: args[2],
        dataType: "json",
        success: function (data) {
            args[args.length - 1](true, data);
        },
        error: function (xhr, textStatus, responseText) {
            console.log(xhr, textStatus, responseText);
            args[args.length - 1](false, textStatus);
        }
    });
}


$(function () {
    var save_box = '<div style="position:fixed;bottom:0;width:100%; text-align:center; z-index:999; padding: 18px 0;background-color: #333;"><input type="button" class="save" value="保存" style="width: 130px;height: 42px;font-size: 16px; background-color: #36c6d3;color:#ffffff;" /></div>';
    
    var img_obj;
    $("body").append(save_box);

    $("body").css("padding-bottom", 78);
    
    $("[data-field]").attr("contenteditable", "true");

    if ($(".file-upload").length > 0) {


        $(".file-upload").Uploads({
            url: appConfig.adminPath + "ashx/upload.ashx",
            process: function (process, i, fileList, field) {

            },
            success: function (flag, val, i, field) {
                console.log(flag, val, i, field);
                if (flag) {
                    img_obj.attr("src", val[0]);
                }
                else {

                }
            },
            before: function (fileList, field) {
            },
            complate: function (field) {

            }
        });
    }
    if ($('.commonweb img').length > 0) {


        $('.commonweb img').contextMenu('myMenu_Img', {
            bindings: {
                'change': function (t) {
                    img_obj = $(t);
                    $(".file-upload").click();
                },
                'remove': function (t) {
                    $(t).remove();
                }
            }
        });
    }
    
    ///暂时只支持
    $(".save").on("click", function () {
        var model = {};
        $("[data-field]").each(function (i, e) {
            //if ($(e).attr("data-type").toUpperCase() == "EDITOR") {
            //    model[$(e).attr("data-field")] = encodeURIComponent("_FLAG_CONTENT_ENCODE_" + $(e).html());
            //}
            //else {
            //    model[$(e).attr("data-field")] = $(e).html();
            //}
            model[$(e).attr("data-field")] = StrEnCode($(e).html());
        })
        if (ClassId > 0) {
            model["ClassId"] = ClassId;
        }
        if (Id > 0) {
            model["Id"] = Id;
        }

        var data = { model: JSON.stringify(model), table: Table };
        var action = Id > 0 ? "editArticle" : "addArticle";

        AjaxSubmit(appConfig.adminPath + "ashx/visualedit.ashx", action, data, function (f, d) {
            //$(_this).button('reset');
            if (f) {
                //请求成功
                if (parseInt(d.result) > 0) {
                    
                    alert("保存成功", function () {
                        if (Table == "SinglePage") {
                            window.location.href = window.location.href;
                        }
                        else {
                            window.location.href = WebPath + "edit/" + d.result + ".html";
                        }
                    });
                    //alert("操作成功", function () {
                    //    if (IsEditAction() || IsAddAction()) { $.Cookie_Del(AutoSave_CookieName()) }
                    //    setTimeout(function () {
                    //        JumpList();
                    //    }, 500);
                    //});
                }
                else if (parseInt(d.result) == -99) {
                    alert("登录超时 , 请重新登录", function () {
                        setTimeout(function () {
                            window.location.href = appConfig.adminPath + "login.aspx";
                        }, 500);
                    });
                }
                else {
                    alert("操作失败 , 请稍后重试 !");
                }
            }
            else {
                //请求失败
                alert("请求出错 , 请稍后刷新重试 !");
            }
        });

    });

})